<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>heap.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_structs_heap.js.html">heap.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Datastructure: Heap.
<a name="line17"></a> *
<a name="line18"></a> *
<a name="line19"></a> * This file provides the implementation of a Heap datastructure. Smaller keys
<a name="line20"></a> * rise to the top.
<a name="line21"></a> *
<a name="line22"></a> * The big-O notation for all operations are below:
<a name="line23"></a> * &lt;pre&gt;
<a name="line24"></a> *  Method          big-O
<a name="line25"></a> * ----------------------------------------------------------------------------
<a name="line26"></a> * - insert         O(logn)
<a name="line27"></a> * - remove         O(logn)
<a name="line28"></a> * - peek           O(1)
<a name="line29"></a> * - contains       O(n)
<a name="line30"></a> * &lt;/pre&gt;
<a name="line31"></a> */
<a name="line32"></a>// TODO(user): Should this rely on natural ordering via some Comparable
<a name="line33"></a>//     interface?
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>goog.provide(&#39;goog.structs.Heap&#39;);
<a name="line37"></a>
<a name="line38"></a>goog.require(&#39;goog.array&#39;);
<a name="line39"></a>goog.require(&#39;goog.object&#39;);
<a name="line40"></a>goog.require(&#39;goog.structs.Node&#39;);
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/**
<a name="line45"></a> * Class for a Heap datastructure.
<a name="line46"></a> *
<a name="line47"></a> * @param {goog.structs.Heap|Object=} opt_heap Optional goog.structs.Heap or
<a name="line48"></a> *     Object to initialize heap with.
<a name="line49"></a> * @constructor
<a name="line50"></a> */
<a name="line51"></a>goog.structs.Heap = function(opt_heap) {
<a name="line52"></a>  /**
<a name="line53"></a>   * The nodes of the heap.
<a name="line54"></a>   * @private
<a name="line55"></a>   * @type {Array.&lt;goog.structs.Node&gt;}
<a name="line56"></a>   */
<a name="line57"></a>  this.nodes_ = [];
<a name="line58"></a>
<a name="line59"></a>  if (opt_heap) {
<a name="line60"></a>    this.insertAll(opt_heap);
<a name="line61"></a>  }
<a name="line62"></a>};
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * Insert the given value into the heap with the given key.
<a name="line67"></a> * @param {*} key The key.
<a name="line68"></a> * @param {*} value The value.
<a name="line69"></a> */
<a name="line70"></a>goog.structs.Heap.prototype.insert = function(key, value) {
<a name="line71"></a>  var node = new goog.structs.Node(key, value);
<a name="line72"></a>  var nodes = this.nodes_;
<a name="line73"></a>  nodes.push(node);
<a name="line74"></a>  this.moveUp_(nodes.length - 1);
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Adds multiple key-value pairs from another goog.structs.Heap or Object
<a name="line80"></a> * @param {goog.structs.Heap|Object} heap Object containing the data to add.
<a name="line81"></a> */
<a name="line82"></a>goog.structs.Heap.prototype.insertAll = function(heap) {
<a name="line83"></a>  var keys, values;
<a name="line84"></a>  if (heap instanceof goog.structs.Heap) {
<a name="line85"></a>    keys = heap.getKeys();
<a name="line86"></a>    values = heap.getValues();
<a name="line87"></a>
<a name="line88"></a>    // If it is a heap and the current heap is empty, I can realy on the fact
<a name="line89"></a>    // that the keys/values are in the correct order to put in the underlying
<a name="line90"></a>    // structure.
<a name="line91"></a>    if (heap.getCount() &lt;= 0) {
<a name="line92"></a>      var nodes = this.nodes_;
<a name="line93"></a>      for (var i = 0; i &lt; keys.length; i++) {
<a name="line94"></a>        nodes.push(new goog.structs.Node(keys[i], values[i]));
<a name="line95"></a>      }
<a name="line96"></a>      return;
<a name="line97"></a>    }
<a name="line98"></a>  } else {
<a name="line99"></a>    keys = goog.object.getKeys(heap);
<a name="line100"></a>    values = goog.object.getValues(heap);
<a name="line101"></a>  }
<a name="line102"></a>
<a name="line103"></a>  for (var i = 0; i &lt; keys.length; i++) {
<a name="line104"></a>    this.insert(keys[i], values[i]);
<a name="line105"></a>  }
<a name="line106"></a>};
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * Retrieves and removes the root value of this heap.
<a name="line111"></a> * @return {*} The value removed from the root of the heap.  Returns
<a name="line112"></a> *     undefined if the heap is empty.
<a name="line113"></a> */
<a name="line114"></a>goog.structs.Heap.prototype.remove = function() {
<a name="line115"></a>  var nodes = this.nodes_;
<a name="line116"></a>  var count = nodes.length;
<a name="line117"></a>  var rootNode = nodes[0];
<a name="line118"></a>  if (count &lt;= 0) {
<a name="line119"></a>    return undefined;
<a name="line120"></a>  } else if (count == 1) {
<a name="line121"></a>    goog.array.clear(nodes);
<a name="line122"></a>  } else {
<a name="line123"></a>    nodes[0] = nodes.pop();
<a name="line124"></a>    this.moveDown_(0);
<a name="line125"></a>  }
<a name="line126"></a>  return rootNode.getValue();
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Retrieves but does not remove the root value of this heap.
<a name="line132"></a> * @return {*} The value at the root of the heap. Returns
<a name="line133"></a> *     undefined if the heap is empty.
<a name="line134"></a> */
<a name="line135"></a>goog.structs.Heap.prototype.peek = function() {
<a name="line136"></a>  var nodes = this.nodes_;
<a name="line137"></a>  if (nodes.length == 0) {
<a name="line138"></a>    return undefined;
<a name="line139"></a>  }
<a name="line140"></a>  return nodes[0].getValue();
<a name="line141"></a>};
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>/**
<a name="line145"></a> * Retrieves but does not remove the key of the root node of this heap.
<a name="line146"></a> * @return {*} The key at the root of the heap. Returns undefined if the
<a name="line147"></a> *     heap is empty.
<a name="line148"></a> */
<a name="line149"></a>goog.structs.Heap.prototype.peekKey = function() {
<a name="line150"></a>  return this.nodes_[0] &amp;&amp; this.nodes_[0].getKey();
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Moves the node at the given index down to its proper place in the heap.
<a name="line156"></a> * @param {number} index The index of the node to move down.
<a name="line157"></a> * @private
<a name="line158"></a> */
<a name="line159"></a>goog.structs.Heap.prototype.moveDown_ = function(index) {
<a name="line160"></a>  var nodes = this.nodes_;
<a name="line161"></a>  var count = nodes.length;
<a name="line162"></a>
<a name="line163"></a>  // Save the node being moved down.
<a name="line164"></a>  var node = nodes[index];
<a name="line165"></a>  // While the current node has a child.
<a name="line166"></a>  while (index &lt; (count &gt;&gt; 1)) {
<a name="line167"></a>    var leftChildIndex = this.getLeftChildIndex_(index);
<a name="line168"></a>    var rightChildIndex = this.getRightChildIndex_(index);
<a name="line169"></a>
<a name="line170"></a>    // Determine the index of the smaller child.
<a name="line171"></a>    var smallerChildIndex = rightChildIndex &lt; count &amp;&amp;
<a name="line172"></a>        nodes[rightChildIndex].getKey() &lt; nodes[leftChildIndex].getKey() ?
<a name="line173"></a>        rightChildIndex : leftChildIndex;
<a name="line174"></a>
<a name="line175"></a>    // If the node being moved down is smaller than its children, the node
<a name="line176"></a>    // has found the correct index it should be at.
<a name="line177"></a>    if (nodes[smallerChildIndex].getKey() &gt; node.getKey()) {
<a name="line178"></a>      break;
<a name="line179"></a>    }
<a name="line180"></a>
<a name="line181"></a>    // If not, then take the smaller child as the current node.
<a name="line182"></a>    nodes[index] = nodes[smallerChildIndex];
<a name="line183"></a>    index = smallerChildIndex;
<a name="line184"></a>  }
<a name="line185"></a>  nodes[index] = node;
<a name="line186"></a>};
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * Moves the node at the given index up to its proper place in the heap.
<a name="line191"></a> * @param {number} index The index of the node to move up.
<a name="line192"></a> * @private
<a name="line193"></a> */
<a name="line194"></a>goog.structs.Heap.prototype.moveUp_ = function(index) {
<a name="line195"></a>  var nodes = this.nodes_;
<a name="line196"></a>  var node = nodes[index];
<a name="line197"></a>
<a name="line198"></a>  // While the node being moved up is not at the root.
<a name="line199"></a>  while (index &gt; 0) {
<a name="line200"></a>    // If the parent is less than the node being moved up, move the parent down.
<a name="line201"></a>    var parentIndex = this.getParentIndex_(index);
<a name="line202"></a>    if (nodes[parentIndex].getKey() &gt; node.getKey()) {
<a name="line203"></a>      nodes[index] = nodes[parentIndex];
<a name="line204"></a>      index = parentIndex;
<a name="line205"></a>    } else {
<a name="line206"></a>      break;
<a name="line207"></a>    }
<a name="line208"></a>  }
<a name="line209"></a>  nodes[index] = node;
<a name="line210"></a>};
<a name="line211"></a>
<a name="line212"></a>
<a name="line213"></a>/**
<a name="line214"></a> * Gets the index of the left child of the node at the given index.
<a name="line215"></a> * @param {number} index The index of the node to get the left child for.
<a name="line216"></a> * @return {number} The index of the left child.
<a name="line217"></a> * @private
<a name="line218"></a> */
<a name="line219"></a>goog.structs.Heap.prototype.getLeftChildIndex_ = function(index) {
<a name="line220"></a>  return index * 2 + 1;
<a name="line221"></a>};
<a name="line222"></a>
<a name="line223"></a>
<a name="line224"></a>/**
<a name="line225"></a> * Gets the index of the right child of the node at the given index.
<a name="line226"></a> * @param {number} index The index of the node to get the right child for.
<a name="line227"></a> * @return {number} The index of the right child.
<a name="line228"></a> * @private
<a name="line229"></a> */
<a name="line230"></a>goog.structs.Heap.prototype.getRightChildIndex_ = function(index) {
<a name="line231"></a>  return index * 2 + 2;
<a name="line232"></a>};
<a name="line233"></a>
<a name="line234"></a>
<a name="line235"></a>/**
<a name="line236"></a> * Gets the index of the parent of the node at the given index.
<a name="line237"></a> * @param {number} index The index of the node to get the parent for.
<a name="line238"></a> * @return {number} The index of the parent.
<a name="line239"></a> * @private
<a name="line240"></a> */
<a name="line241"></a>goog.structs.Heap.prototype.getParentIndex_ = function(index) {
<a name="line242"></a>  return (index - 1) &gt;&gt; 1;
<a name="line243"></a>};
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>/**
<a name="line247"></a> * Gets the values of the heap.
<a name="line248"></a> * @return {Array} The values in the heap.
<a name="line249"></a> */
<a name="line250"></a>goog.structs.Heap.prototype.getValues = function() {
<a name="line251"></a>  var nodes = this.nodes_;
<a name="line252"></a>  var rv = [];
<a name="line253"></a>  var l = nodes.length;
<a name="line254"></a>  for (var i = 0; i &lt; l; i++) {
<a name="line255"></a>    rv.push(nodes[i].getValue());
<a name="line256"></a>  }
<a name="line257"></a>  return rv;
<a name="line258"></a>};
<a name="line259"></a>
<a name="line260"></a>
<a name="line261"></a>/**
<a name="line262"></a> * Gets the keys of the heap.
<a name="line263"></a> * @return {Array} The keys in the heap.
<a name="line264"></a> */
<a name="line265"></a>goog.structs.Heap.prototype.getKeys = function() {
<a name="line266"></a>  var nodes = this.nodes_;
<a name="line267"></a>  var rv = [];
<a name="line268"></a>  var l = nodes.length;
<a name="line269"></a>  for (var i = 0; i &lt; l; i++) {
<a name="line270"></a>    rv.push(nodes[i].getKey());
<a name="line271"></a>  }
<a name="line272"></a>  return rv;
<a name="line273"></a>};
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>/**
<a name="line277"></a> * Whether the heap contains the given value.
<a name="line278"></a> * @param {Object} val The value to check for.
<a name="line279"></a> * @return {boolean} Whether the heap contains the value.
<a name="line280"></a> */
<a name="line281"></a>goog.structs.Heap.prototype.containsValue = function(val) {
<a name="line282"></a>  return goog.array.some(this.nodes_, function(node) {
<a name="line283"></a>    return node.getValue() == val;
<a name="line284"></a>  });
<a name="line285"></a>};
<a name="line286"></a>
<a name="line287"></a>
<a name="line288"></a>/**
<a name="line289"></a> * Whether the heap contains the given key.
<a name="line290"></a> * @param {Object} key The key to check for.
<a name="line291"></a> * @return {boolean} Whether the heap contains the key.
<a name="line292"></a> */
<a name="line293"></a>goog.structs.Heap.prototype.containsKey = function(key) {
<a name="line294"></a>  return goog.array.some(this.nodes_, function(node) {
<a name="line295"></a>    return node.getKey() == key;
<a name="line296"></a>  });
<a name="line297"></a>};
<a name="line298"></a>
<a name="line299"></a>
<a name="line300"></a>/**
<a name="line301"></a> * Clones a heap and returns a new heap
<a name="line302"></a> * @return {goog.structs.Heap} A new goog.structs.Heap with the same key-value
<a name="line303"></a> *     pairs.
<a name="line304"></a> */
<a name="line305"></a>goog.structs.Heap.prototype.clone = function() {
<a name="line306"></a>  return new goog.structs.Heap(this);
<a name="line307"></a>};
<a name="line308"></a>
<a name="line309"></a>
<a name="line310"></a>/**
<a name="line311"></a> * The number of key-value pairs in the map
<a name="line312"></a> * @return {number} The number of pairs.
<a name="line313"></a> */
<a name="line314"></a>goog.structs.Heap.prototype.getCount = function() {
<a name="line315"></a>  return this.nodes_.length;
<a name="line316"></a>};
<a name="line317"></a>
<a name="line318"></a>
<a name="line319"></a>/**
<a name="line320"></a> * Returns true if this heap contains no elements.
<a name="line321"></a> * @return {boolean} Whether this heap contains no elements.
<a name="line322"></a> */
<a name="line323"></a>goog.structs.Heap.prototype.isEmpty = function() {
<a name="line324"></a>  return goog.array.isEmpty(this.nodes_);
<a name="line325"></a>};
<a name="line326"></a>
<a name="line327"></a>
<a name="line328"></a>/**
<a name="line329"></a> * Removes all elements from the heap.
<a name="line330"></a> */
<a name="line331"></a>goog.structs.Heap.prototype.clear = function() {
<a name="line332"></a>  goog.array.clear(this.nodes_);
<a name="line333"></a>};
</pre>


</body>
</html>
